/*
* @Author: suddev
* @Date:   2017-08-21 10:19:36
* @Last Modified by:   suddev
* @Last Modified time: 2017-08-21 11:09:55
*/
'use strict';

//表单里的错误提示
var formError = {
    show    : function(errorMsg){
        $('.error-item').show().find('.error-message').text(errorMsg);
    },
    hide    : function(){
        $('.error-item').hide().find('.error-message').text('');
    }
};

//page逻辑部分
var page = {
    customerInfo : {

    },
    init : function () {
        this.bindEvent();
    },
    bindEvent : function () {
        var _this = this;
        //登陆按钮的点击
        $('#search').click(function(){
            $('.customer_info_form').hide();
            _this.submit();
        });
    },
    // 提交表单
    submit : function () {
        var formData = {
        };
        if (!($.trim($('#callId').val()) === "")) {
        	formData.callId = $.trim($('#callId').val());
        }
        if (!($.trim($('#customerId').val()) === "")) {
        	formData.customerId = $.trim($('#customerId').val());
        }
        if (!($.trim($('#name').val()) === "")) {
        	formData.name = $.trim($('#name').val());
        }
        if (!($.trim($('#callTime').val()) === "")) {
        	formData.callTime = $.trim($('#callTime').val());
        }
        //表单验证结果
        var validateResult = this.formValidate(formData);
        // 验证成功
        if (validateResult.status) {
        	formError.hide();
            //提交
            _income.income_search(formData,function(res){
                var text = "{{#list}}<tr><td>{{callId}}</td><td>{{customerId}}</td><td>{{customerName}}</td><td>{{callTime}}</td><td>{{callMemo}}</td></tr>{{/list}}";
                var template = {
                    list : res
                }
                var result = _tools.renderHtml(text,template);
                $('#info_list').html(result);
                $('.customer_info_list').show();
            },function(errorMsg){
                $('.customer_info_list').hide();
                formError.show(errorMsg);
            });
        }
        //验证失败
        else{
            // 错误提示
            formError.show(validateResult.msg);
            $('.customer_info_list').hide();
        }
    },
    // 表单验证
    formValidate : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        if (   !_tools.validate(formData.callId,'require') 
        	&& !_tools.validate(formData.customerId,'require')
        	&& !_tools.validate(formData.name,'require')
        	&& !_tools.validate(formData.callTime,'require')) {
            result.msg = '请输入查询条件';
            return result;
        }
        //通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    }
}

$(function () {
   page.init(); 
});